package com.loong.musicdemo;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.loong.musicdemo.util.enumutil.LoginClickType;
import com.loong.musicdemo.util.httpUtil.LoginUtil;
import com.loong.musicdemo.widget.iconbtn.IconButton;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private List<IconButton> iconButtons = new ArrayList<>();//登录方式按钮列表
    private LoginClickType type;//登录方式
    private TextView typeTextView;//登录方式文本框
    private LinearLayout loginTypeLinearLayout;//登录方式按钮的父容器

    private EditText editTextAccount;//账号
    private EditText editTextPassWord;//密码
    public LoginActivity() {
    }

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Objects.requireNonNull(getSupportActionBar()).hide();
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        //初始化界面
        type = LoginClickType.EMAIL;//默认登录方式为手机号码登录
        typeTextView = findViewById(R.id.login_type);
        typeTextView.setText(type.getType() + "：");
        loginTypeLinearLayout = findViewById(R.id.login_type_linear_layout);

        editTextAccount = findViewById(R.id.account);
        editTextPassWord = findViewById(R.id.password);
        //登录按钮
        Button buttonLogin = findViewById(R.id.login_button);
        buttonLogin.setOnClickListener(this);
        //获取缓存信息，如果不存在缓存信息，则登录界面，否则进入MainActivity
        SharedPreferences userInfo = getSharedPreferences("userInfo", MODE_PRIVATE);
        if (userInfo.getString("userId", null) != null) {
            String account = userInfo.getString("account", "");
            String pwd = userInfo.getString("pwd", "");
            String type = userInfo.getString("type", "");
            new Thread(() -> {
                String code = LoginUtil.login(account, pwd, type, this);
                login(code);
            }).start();
        }
        //初始化登录方式
        initLoginType();
    }

    /**
     * 初始化登录方式
     */
    private void initLoginType() {
        //登录方式列表
        List<LoginType> loginTypes = Arrays.asList(
                new LoginType("微信", R.drawable.login_type_weichat, LoginClickType.OPEN_APP),
                new LoginType("QQ", R.drawable.login_type_qq, LoginClickType.OPEN_APP),
                new LoginType("手机", R.drawable.login_type_phone, LoginClickType.PHONE),
                new LoginType("邮箱", R.drawable.login_type_netease, LoginClickType.EMAIL));

        for (LoginType loginType : loginTypes) {
            IconButton iconButton = new IconButton(this);
            iconButton.setIcon(loginType.getSrcResId())
                    .setName(loginType.getTypeName())
                    .setOnClickListener(this, loginType.getClickType());//初始化图标按钮
            iconButtons.add(iconButton);
            loginTypeLinearLayout.addView(iconButton);
        }
    }

    /**
     * 修改登录方式
     */
    @SuppressLint("SetTextI18n")
    public void changeLoginType(LoginClickType type) {
        if (this.type == type)
            //未修改则直接返回
            return;
        //修改登录方式
        this.type = type;
        typeTextView.setText(type.getType() + ":");
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.login_button) {
            new Thread(() -> {
                String loginCode = LoginUtil.login(editTextAccount.getText().toString(), editTextPassWord.getText().toString(), type.getType(), this);
                login(loginCode);
            }).start();

        }
    }
    private void login(String loginCode){
        if (loginCode.equals("200")) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);//登录成功
            this.finish();
        } else if (loginCode.equals("400")) {//登录失败
            runOnUiThread(() -> {
                Toast.makeText(LoginActivity.this, "登录失败，请重新尝试！", Toast.LENGTH_SHORT).show();
            });
        } else {//账号不存在或者密码错误
            runOnUiThread(() -> {
                Toast.makeText(LoginActivity.this, "账号不存在或者密码错误，请重新确认！", Toast.LENGTH_SHORT).show();
            });
        }
    }
}

class LoginType {
    private static int typeId = 1;
    private int id;
    private String typeName;
    private int srcResId;
    private LoginClickType clickType;

    LoginType(String typeName, int srcResId, LoginClickType clickType) {
        this.id = autoIncrease();
        this.typeName = typeName;
        this.srcResId = srcResId;
        this.clickType = clickType;
    }

    public static int getTypeId() {
        return typeId;
    }

    String getTypeName() {
        return typeName;
    }

    int getSrcResId() {
        return srcResId;
    }

    LoginClickType getClickType() {
        return clickType;
    }

    private int autoIncrease() {
        return typeId++;
    }
}
